mui.init({
	swipeBack:true,
	pullRefresh: {
		container: '#pullrefresh',
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});

//var shopId = 2177
Vue.use(VueLazyload, {
	preLoad: 1.3,
	error: '../../images/noimg.jpg',
	loading: '../../images/noimg.jpg'
});
var shop = new Vue({
	el: '#shop',
	data: {
		imgCommonUrl:common.imageUrl,
		shopCls: [
			'首页',
			'全部商品',
			'新品'
		],
		shopClsCheck: 0,
		//排序
		sortArr: [{
			text: '全部',
			val: 'recDate'
		}, {
			text: '销量',
			val: 'buys'
		}, {
			text: '价格排序',
			val: 'cash'
		}, {
			text: '人气',
			val: 'comments'
		}],
		sortNum: 0,
		orders: 'asc',
		shopDetail: '',
		isFav: false,
		score: '',
		shopList: [],
		pageNum: 1,
		shopId:''
	},
	methods: {
		//排序
		clsSort: function(index) {
			if(shop.sortNum == index) {
				shop.orders = shop.orders == 'desc' ? 'asc' : 'desc'
			} else {
				shop.sortNum = index
				shop.orders = 'asc'
			}
			shop.pageNum = 1
			shop.shopList = []
			mui('#pullrefresh').pullRefresh().refresh(true);
			mui('#pullrefresh').pullRefresh().pullupLoading();
			mui('#pullrefresh').pullRefresh().scrollTo(0, 0,0.1);
			cssBackGround()
		},
		//点击切换
		shopClsTap: function(index) {
			if(shop.shopClsCheck == index) {
				return
			}
			shop.shopClsCheck = index
			if(shop.shopClsCheck == 1) {
				shop.pageNum = 1
				shop.shopList = []
				shop.orders = 'asc'
				shop.sortNum = 0
				mui('#pullrefresh').pullRefresh().refresh(true);
			}
			cssBackGround()
			mui('#pullrefresh').pullRefresh().pullupLoading();
			mui('#pullrefresh').pullRefresh().scrollTo(0, 0,0.1);
		},
		//收藏
		addInterest: function() {
			common.ajax('/shop/favoriteShopOverlay/'+ shop.shopId, {
			}, function(data) {
				if(data.code == 1) {
					shop.isFav = !shop.isFav
					if(shop.isFav) {
						common.toast('收藏成功')
					} else {
						common.toast('取消收藏')
					}
				} else {
					if(data.resultMsg) {
						common.toast(data.resultMsg)
					}
				}
			})
		},
		openDetail: function(data) {
			//plus.webview.getWebviewById('../details/details.html').close('none')
			common.open('details.html', 'details.html', data.prodId)
		},
		openSeatch: function() {
			var wp = plus.webview.create('../seach/shopSeach.html', '../seach/shopSeach.html', {
				scrollIndicator: 'none',
				scalable: false,
				popGesture: 'none'
			}, {
				shopId: shop.shopDetail.shopId
			})
			wp.show('fade-in');

		},
		//客服
		serviceCenter: function() {
			mui.openWindow({
				id: "../mask/nesting.html",
				url: '../mask/nesting.html',
				extras: {
					url: 'http://p.qiao.baidu.com/cps/chat?siteId=11390067&userId=24711168'
				}
			});
		}
	}
})






mui.plusReady(function() {
	var self = plus.webview.currentWebview();
	var shopId = self.parameter
	shop.shopId = shopId
	if(mui.os.plus) {
	mui.plusReady(function() {
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().pullupLoading();
		}, 10);

	});
} else {
	mui.ready(function() {
		mui('#pullrefresh').pullRefresh().pullupLoading();
	});
}
})









function pullupRefresh() {
	var urls = ['/shop/index/', '/shop/storeProdctList/', '/shop/newProd/']
	var data = {}
	if(shop.shopClsCheck == 1) {
		var temp = shop.sortNum == 0 ? '' : shop.sortArr[shop.sortNum].val + ',' + shop.orders
		data = {
			curPageNO: shop.pageNum++,
			order: temp,
		}
	}
	common.ajax(urls[shop.shopClsCheck] + shop.shopId, data, function(datas) {
		if(datas.code == 1) {
			if(shop.shopClsCheck == 1) {
				shop.shopList = shop.shopList.concat(datas.data.list)
				if(datas.data.pageCount < shop.pageNum) {
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
				} else {
					mui('#pullrefresh').pullRefresh().endPullupToRefresh()
				}
				$(".mui-pull-bottom-pocket").css("display", 'block !important')

			} else {
				$(".mui-pull-bottom-pocket").css("display", 'none !important')
				mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
				if(datas.data.shopDetail) {
					shop.shopDetail = datas.data.shopDetail
					shop.isFav = datas.data.isFav
					shop.score = datas.data.score
				}
				shop.shopList = datas.data.hotViewList || datas.data.list
			}

			shop.$nextTick(function() {
				$(".shop_list_img").height($(".shop_list_img").width())
			})

		} else {
			if(datas.resultMsg) {
				common.toast(datas.resultMsg)
			}
			mui('#pullrefresh').pullRefresh().endPullupToRefresh()
		}
	}, true, 'get')
}


var scroll = 1

////头部监听滚动渐变
window.onscroll = function() { //为了保证兼容性，这里取两个值，哪个有值取哪一个　　
	var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
	scroll = scrollTop
	if(scroll <= 0) {
		return
	}
	var rgba = Math.abs(scrollTop) / 120
	if(scrollTop <= 44) {
		cssBackGround()
	} else {
		$('.mui-bar-nav').css('background', 'rgba(245, 44, 61,' + rgba + ')')
	}
}

mui("#pullrefresh")[0].addEventListener("scroll", function(e) {
	if(scroll > 0) {
		return
	}
	var rgba = Math.abs(e.detail.y) / 120
	if(Math.abs(e.detail.y) <= 44) {
		cssBackGround()
	} else {
		$('.mui-bar-nav').css('background', 'rgba(245, 44, 61,' + rgba + ')')
	}
});

function cssBackGround() {
	$('.mui-bar-nav').css({
		'background': '-webkit-linear-gradient(#a0a0a0, transparent)',
		'background': '-o-linear-gradient(#a0a0a0, transparent)',
		'background': '-moz-linear-gradient(#a0a0a0, transparent)',
		'background': 'linear-gradient(#a0a0a0, transparent)'
	})
}